import Axios from 'axios'
var Config={
    baseURL: '#',
    timeout: 10000,
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'token': window.localStorage.token ? window.localStorage.token : ''
    }
}
// 数据请求之前
Axios.interceptors.request.use((config) => {
    if (
        window.localStorage.token !== undefined &&
        window.localStorage.token !== 'undefined' &&
        window.localStorage.token !== '' &&
        window.localStorage.token !== null) {
        config.headers.token = window.localStorage.token;
    } else {
        console.info('token not found');
    }
    return config;
});
// 提交成功并返回数据时
Axios.interceptors.response.use(function (response) {
    return response;
}, function (error) {
    return Promise.reject(error);
});
class AjaxFile {
    static ResponseHandle(F, res) {
        switch (res.data.error) {
            case 1:
                Notification({
                    title: '请求提示',
                    message: res.data.msg,
                    type: 'warning'
                });
                F.reject(res.data.msg);
                break;
            case 2:
                window.localStorage.user_status = 'outlogin';
                App.$router.push('/login');
                break;
            default:
                F.resolve(res.data.data);
                break;
        }
    }
    static Post(url, data) {
        var F = new Future();
        Axios.post(url, data,Config)
            .then((res) => {
                AjaxFile.ResponseHandle(F, res);
            })
            .catch((e) => {
                console.info(e);
                F.reject(e);
            });
        return F;
    }
    static Get(url) {
        var F = new Future();
        Axios.get(url, Config)
            .then((res) => {
                AjaxFile.ResponseHandle(F, res);
            })
            .catch((e) => {
                F.reject(e);
            });
        return F;
    }
}

export default AjaxFile;